/*
 * scim-dbus-frontend.cpp
 *
 *  Created on: 2008-7-6
 *      Author: zarra
 */

#include "scim-dbus-frontend.h"

#include <signal.h>
#include "tools.h"
static const char* SERVER_NAME = "org.scim.frontend";
static const char* SERVER_PATH = "/org/scim/frontend";

static inline scim::String toUTF8(const scim::WideString& str){
	return scim::utf8_wcstombs(str);
}


ScimSide::ScimSide(const scim::BackEndPointer &backend) :
	scim::FrontEndBase(backend) {
}

void ScimSide::show_preedit_string(int id) {
	if (m_dbus_side)
		m_dbus_side->show_preedit_string(id);
}

void ScimSide::show_aux_string(int id) {
	m_dbus_side->show_aux_string(id);
}

void ScimSide::show_lookup_table(int id) {
	m_dbus_side->show_lookup_table(id);
}

void ScimSide::hide_preedit_string(int id) {
	m_dbus_side->hide_preedit_string(id);
}

void ScimSide::hide_aux_string(int id) {
	m_dbus_side->hide_aux_string(id);
}

void ScimSide::hide_lookup_table(int id) {

	m_dbus_side->hide_lookup_table(id);
}

void ScimSide::update_preedit_caret(int id, int caret) {
	
	m_dbus_side->update_preedit_caret(id, caret);
}

void ScimSide::update_preedit_string(int id, const scim::WideString & str,
		const scim::AttributeList & attrs) {
	
	m_dbus_side->update_preedit_string(id, toUTF8(str));
	printf("update_preedit_string id:%d str:%S\n",id,str.c_str());
}

void ScimSide::update_aux_string(int id, const scim::WideString & str,
		const scim::AttributeList & attrs) {
	m_dbus_side->update_aux_string(id, toUTF8(str));
	printf("update_aux_string id:%d str:%S\n",id,str.c_str());

}

void ScimSide::update_lookup_table(int id, const scim::LookupTable & table) {
	std::vector< ::DBus::String> v;
	size_t n = table.number_of_candidates();
	for (size_t i = 0; i < n; ++i) {
		v.push_back(toUTF8(table.get_candidate(i)));
	}
	m_dbus_side->update_lookup_table(id, v);
	printf("update_lookup_table:%d\n",id);
}

void ScimSide::commit_string(int id, const scim::WideString & str) {
	m_dbus_side->commit_string(id, toUTF8(str));
}

void ScimSide::forward_key_event(int id, const scim::KeyEvent & key) {

}

void ScimSide::register_properties(int id,
		const scim::PropertyList & properties) {

}

void ScimSide::update_property(int id, const scim::Property & property) {

}

void ScimSide::beep(int id) {
	m_dbus_side->beep(id);
}

//void ScimSide::start_helper(int id, const String &helper_uuid){}
//
//void ScimSide::stop_helper(int id, const String &helper_uuid){}
//
//void ScimSide::send_helper_event(int id, const String &helper_uuid,
//		const Transaction &trans){}
//
//bool ScimSide::get_surrounding_text(int id, WideString &text, int &cursor,
//		int maxlen_before, int maxlen_after){}
//
//bool ScimSide::delete_surrounding_text(int id, int offset, int len){}
/***************************
 * ******************************
 */
//DBusSide::

DBusSide::DBusSide(DBus::Connection& connection, ScimSide* scimside) :
	DBus::ObjectAdaptor(connection, SERVER_PATH), m_scim_side(scimside) {
}
void DBusSide::get_factory_list_for_encoding(const ::DBus::String& encoding,
		std::vector< ::DBus::String>& uuids, ::DBus::UInt32& count) {

	count = m_scim_side->get_factory_list_for_encoding(uuids, get_validate_encoding(encoding));
}

void DBusSide::get_factory_list_for_language(const ::DBus::String& language,
		std::vector< ::DBus::String>& uuids, ::DBus::UInt32& count) {
	count = m_scim_side->get_factory_list_for_language(uuids, language);

}

::DBus::String DBusSide::get_default_factory(const ::DBus::String& language,
		const ::DBus::String& encoding) {
	return m_scim_side->get_default_factory(language, get_validate_encoding(encoding));

}

void DBusSide::set_default_factory(const ::DBus::String& language,
		const ::DBus::String& uuid) {
	m_scim_side->set_default_factory(language, uuid);

}

::DBus::String DBusSide::get_next_factory(const ::DBus::String& language,
		const ::DBus::String& encoding, const ::DBus::String& cur_uuid) {
	return m_scim_side->get_next_factory(language, get_validate_encoding(encoding), cur_uuid);

}

::DBus::String DBusSide::get_previous_factory(const ::DBus::String& language,
		const ::DBus::String& encoding, const ::DBus::String& cur_uuid) {
	return m_scim_side->get_previous_factory(language, get_validate_encoding(encoding), cur_uuid);

}

::DBus::String DBusSide::get_factory_name(const ::DBus::String& uuid) {
	return toUTF8(m_scim_side->get_factory_name(uuid));
}

::DBus::String DBusSide::get_factory_authors(const ::DBus::String& uuid) {
	return toUTF8(m_scim_side->get_factory_authors(uuid));
}

::DBus::String DBusSide::get_factory_credits(const ::DBus::String& uuid) {
	return toUTF8(m_scim_side->get_factory_credits(uuid));
}

::DBus::String DBusSide::get_factory_help(const ::DBus::String& uuid) {
	return toUTF8(m_scim_side->get_factory_help(uuid));
}

::DBus::String DBusSide::get_factory_icon_file(const ::DBus::String& uuid) {
	return m_scim_side->get_factory_icon_file(uuid);
}

::DBus::String DBusSide::get_factory_locales(const ::DBus::String& uuid) {
	return m_scim_side->get_factory_locales(uuid);
}

::DBus::String DBusSide::get_factory_language(const ::DBus::String& uuid) {
	return m_scim_side->get_factory_language(uuid);
}

::DBus::Bool DBusSide::validate_factory(const ::DBus::String& uuid,
		const ::DBus::String& encoding) {
	return m_scim_side->validate_factory(uuid, get_validate_encoding(encoding));
}

::DBus::String DBusSide::get_all_locales() {
	return m_scim_side->get_all_locales();
}

::DBus::Int32 DBusSide::new_instance(const ::DBus::String& sf_uuid,
		const ::DBus::String& encoding) {
	return m_scim_side->new_instance(sf_uuid, get_validate_encoding(encoding));

}

::DBus::Bool DBusSide::replace_instance(const ::DBus::Int32& si_id,
		const ::DBus::String& sf_uuid) {
	return m_scim_side->replace_instance(si_id, sf_uuid);
}

::DBus::Bool DBusSide::delete_instance(const ::DBus::Int32& id) {
	return m_scim_side->delete_instance(id);
}

void DBusSide::delete_all_instances() {
	m_scim_side->delete_all_instances();
}

::DBus::String DBusSide::get_instance_uuid(const ::DBus::Int32& id) {
	return m_scim_side->get_instance_uuid(id);
}

::DBus::String DBusSide::get_instance_encoding(const ::DBus::Int32& id) {
	return m_scim_side->get_instance_encoding(id);
}

::DBus::String DBusSide::get_instance_name(const ::DBus::Int32& id) {
	return toUTF8(m_scim_side->get_instance_name(id));
}

::DBus::String DBusSide::get_instance_authors(const ::DBus::Int32& id) {
	return toUTF8(m_scim_side->get_instance_authors(id));
}

::DBus::String DBusSide::get_instance_credits(const ::DBus::Int32& id) {
	return toUTF8(m_scim_side->get_instance_credits(id));
}

::DBus::String DBusSide::get_instance_help(const ::DBus::Int32& id) {
	return toUTF8(m_scim_side->get_instance_help(id));
}

::DBus::String DBusSide::get_instance_icon_file(const ::DBus::Int32& id) {
	return m_scim_side->get_instance_icon_file(id);
}

::DBus::Bool DBusSide::process_key_event(const ::DBus::Int32& id,
		const ::DBus::String& key) {
	bool b=m_scim_side->process_key_event(id, scim::KeyEvent(key));
	
	return b;
}

void DBusSide::move_preedit_caret(const ::DBus::Int32& id,
		const ::DBus::UInt32& pos) {
	m_scim_side->move_preedit_caret(id, pos);
}

void DBusSide::select_candidate(const ::DBus::Int32& id,
		const ::DBus::UInt32& index) {
	m_scim_side->select_candidate(id, index);
}

void DBusSide::update_lookup_table_page_size(const ::DBus::Int32& id,
		const ::DBus::UInt32& page_size) {
	m_scim_side->update_lookup_table_page_size(id, page_size);
}

void DBusSide::lookup_table_page_up(const ::DBus::Int32& id) {
	m_scim_side->lookup_table_page_up(id);
}

void DBusSide::lookup_table_page_down(const ::DBus::Int32& id) {
	m_scim_side->lookup_table_page_down(id);
}

void DBusSide::reset(const ::DBus::Int32& id) {
	m_scim_side->reset(id);
}

void DBusSide::focus_in(const ::DBus::Int32& id) {
	m_scim_side->focus_in(id);
}

void DBusSide::focus_out(const ::DBus::Int32& id) {

	m_scim_side->focus_out(id);
}

void DBusSide::trigger_property(const ::DBus::Int32& id,
		const ::DBus::String& property) {

}

void DBusSide::update_client_capabilities(const ::DBus::Int32& id,
		const ::DBus::UInt32& cap) {
	m_scim_side->update_client_capabilities(id, cap);
}

DBus::BusDispatcher dispatcher;
void niam(int sig) {
	dispatcher.leave();
}
void ScimSide::init(int argc, char **argv) {
//	scim::String uuid;
//	scim::String language = "zh_CN";
//	scim::String encoding = "UTF-8";
//	uuid = get_default_factory(language, encoding);
//	id = new_instance(uuid, encoding);

	signal(SIGTERM, niam);
	signal(SIGINT, niam);

	DBus::default_dispatcher = &dispatcher;
}
void ScimSide::run() {
//	printf("get id %d\n", id);
//	focus_in(id);

	DBus::Connection conn(DBus::Connection::SessionBus());
	conn.request_name(SERVER_NAME);
	m_dbus_side = new DBusSide(conn, this);

	dispatcher.enter();

}
